package com.ftwj.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import com.ftwj.demo.base.wrapper.QueryWrapper;
import com.ftwj.demo.config.quartz.JobInformation;
import com.ftwj.demo.config.quartz.QuartzConfig;
import com.ftwj.demo.config.quartz.QuartzManager;
import com.ftwj.demo.modular.entity.ScheduleJobBean;
import com.ftwj.demo.modular.service.ScheduleJobService;

@Configuration
public class QuartzListener extends QuartzConfig{
	
	@Autowired
    private QuartzManager quartzManager;
	@Autowired
	private ScheduleJobService scheduleJobService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    	QueryWrapper wrapper=new QueryWrapper();
    	wrapper.ne("state", -1);
		//查询数据库中所有状态正常的调度器
    	List<ScheduleJobBean> list = scheduleJobService.findListByConditions(wrapper);
		for (ScheduleJobBean entity : list) {
			if(entity.getState()==1) {
				JobInformation jobInfo = quartzManager.buildJobInformation(entity.getName(), entity.getGroup(),entity.getDescription(), entity.getCron(), entity.getClassName(), entity.getParams());
				quartzManager.startJobTask(jobInfo);
			}
			if(entity.getState()==0) {
				JobInformation jobInfo = quartzManager.buildJobInformation(entity.getName(), entity.getGroup(),entity.getDescription(), entity.getCron(), entity.getClassName(), entity.getParams());
				quartzManager.startPauseJobTask(jobInfo);
			}
		}
    }
    
}
